<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\index\controller;

use app\common\model\Archives as ArchivesModel;
use app\common\model\Category as CategoryModel;
use app\common\model\Files as FilesModel;

use think\facade\Request;
use think\facade\View;


class Archives extends Base
{

    public function index()
    {
		//获取栏目名
		$name = Request::param('name');
		
		$category = CategoryModel::where('name',$name)->where('status',1)->findOrEmpty();

		if ($category->isEmpty()) {
			return $this->error('此栏目不可访问');
		}

		//获取栏目下所有子栏目ID
		$category_all_ids = CategoryModel::where('status',1)->field('id,pid')->select();
		$category_ids = get_childs_id_str(get_childs_id($category_all_ids,$category->id),$category->id);


		//查询所有文档
		$archives = ArchivesModel::where('category_id','in',$category_ids)->where('status',1)
					->order('sort desc, update_time desc')
					->paginate(10)
					->each(function($item, $key){
						$item->images = $item->files->toArray();
						if(empty($item->images)){
							$item->thumb = '/static/common/images/empty.png';
						}else{
							$item->thumb = $item->images[0]['url'];
						}
					});


		//获取模板名 
		$template =  'list_default';
		if(!empty($category->getData('template_list'))){
			$template = $category->getData('template_list');
		}

	
		View::assign('archives',$archives);
		View::assign('category',$category);

		View::assign('title',$category->title .' - ');
		View::assign('keywords',$category->keywords .',');
		View::assign('description',$category->description);
		return View::fetch($template);
    }

    public function show()
    {
		//获取栏目名
		$name = Request::param('name');
		//获取ID
		$id = Request::param('id');
		
		$category = CategoryModel::where('name',$name)->where('status',1)->findOrEmpty();
		if ($category->isEmpty()) {
			return $this->error('此栏目不可访问');
		}

		//查询所有文档
		$archives = ArchivesModel::where('status',1)->findOrEmpty($id);
		if ($archives->isEmpty()) {
			return $this->error('此文档不可访问');
		}
		$images = $archives->files->toArray();


		//更新浏览量
		$archives->hits += 1;
		$archives->save();

		//获取模板名 
		$template =  'show_default';
		if(!empty($category->getData('template_show'))){
			$template = $category->getData('template_show');
		}
		View::assign('archives',$archives);
		View::assign('images',$images);
		View::assign('category',$category);


		View::assign('title',$archives->title . ' - ' . $category->title .' - ');
		View::assign('keywords',$archives->keywords . ',' . $category->keywords .',');
		View::assign('description',$archives->description);
		return View::fetch($template);
    }


}
